import dataModel from '@/models/panel-config-data.js';

export default {
  data() {
    return {
      siteList: [],
      directoryList: [],
      pageList: [],
    };
  },
  methods: {
    getSiteListNew() {
      dataModel.getSiteListNew()
        .then(list => {
          this.siteList = list;
          const { name, params: { siteId } = {} } = this.$route || {};
          if (name === 'myWorkSpace' && siteId) {
            const auroraSite = list.find(site => site.applicationUuid === siteId);
            if (auroraSite) {
              this.siteList = [auroraSite];
            }
          }
          if (this.form.linkType === 'page') {
            const currSite = this.isAurora ?
              this.siteList[0] :
              this.siteList.find(site => site.applicationUuid === this.form.siteId);
            if (currSite) {
              this.getSiteSingle(currSite.applicationUuid);
            } else {
              this.form.siteId = '';
              this.form.directoryId = '';
              this.form.pageId = '';
              this.form.jumpUrl = '';
            }
          }
        });
    },
    getSiteSingle(siteId) {
      dataModel.getSiteSingle(siteId).then(res => {
        if (!res || res.length === 0) {
          return;
        }
        this.directoryList = res[0].children;
        if (!this.form.directoryId) {
          return;
        }
        const currDirectory = this.directoryList.find(directory => directory.uuid === this.form.directoryId);
        if (currDirectory) {
          this.pageList = currDirectory.children;
          const currPage = this.pageList.find(page => page.uuid === this.form.pageId);
          if (currPage) {
            this.form.jumpUrl = currPage.url;
          } else {
            this.form.pageId = '';
            this.form.jumpUrl = '';
          }
        }
      });
    },

    // 链接类型改变
    linkTypeChange() {
      this.pageList = [];
      this.form.directoryId = '';
      this.form.pageId = '';
      this.form.jumpUrl = '';
      if (this.isAurora) {
        this.form.siteId = this.siteList[0] && this.siteList[0].applicationUuid;
        this.getSiteSingle(this.form.siteId);
      } else {
        this.form.siteId = '';
        this.directoryList = [];
      }
    },

    // 站点改变
    siteChange(val) {
      // 清理旧数据
      this.directoryList = [];
      this.pageList = [];
      this.form.directoryId = '';
      this.form.pageId = '';
      this.form.jumpUrl = '';

      const currSite = this.siteList.find(site => site.applicationUuid === val);
      if (currSite) {
        this.getSiteSingle(currSite.applicationUuid);
      }
    },

    // 目录改变
    directoryChange(val) {
      this.pageList = [];
      this.form.pageId = '';
      this.form.jumpUrl = '';

      const currDirectory = this.directoryList.find(directory => directory.uuid === val);

      if (currDirectory) {
        this.pageList = currDirectory.children;
      }
    },

    // 页面改变
    pageChange(val) {
      this.form.jumpUrl = '';
      const currPage = this.pageList.find(page => page.uuid === val);
      if (currPage) {
        if (this.isAurora) {
          // 替换成洞察云的url：/xvueui/aurora.html#/my-workspace/:siteId/:pageId
          this.form.jumpUrl = `/xvueui/aurora.html#/my-workspace/${currPage.siteUuid}/${currPage.uuid}`;
        } else {
          this.form.jumpUrl = currPage.url;
        }
      }
    },
  },
};
